package com.zmn.oms.admin.controller.report.orderpay;

import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.mcc.cas.web.StaffAdminService;
import com.zmn.mcc.common.dto.role.RoleDRO;
import com.zmn.mcc.dubbo.interfaces.role.RoleListRemoteService;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.common.constant.OrderExportConsts;
import com.zmn.oms.model.entity.report.EsReportPayQuery;
import com.zmn.pay.common.constant.PayConsts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author liuying
 * @date 2021/08/09 16:10
 */
@Slf4j
public class ReportOrderPayBaseController extends OmsBaseController {

    @Reference(version = com.zmn.mcc.dubbo.dto.DubboConsts.INTERFACE_VERSION, check = false)
    private RoleListRemoteService roleListRemoteService;

    static final int QUERY_DATE_TYPE_COMPLETE_TIME = 1; // 完成时间
    static final int QUERY_DATE_TYPE_CHECKOUT_TIME = 2; // 收单时间
    static final int QUERY_DATE_TYPE_ACCOUNT_TIME = 3; // 算账时间
    static final int QUERY_DATE_TYPE_PAY_TIME = 4; // 支付时间

    /**
     * 设置时间
     *
     * @param query
     */
    protected void processQuery(EsReportPayQuery query) {
        // 时间转换
        if (Objects.nonNull(query.getDateType())) {
            switch (query.getDateType()) {
                case QUERY_DATE_TYPE_COMPLETE_TIME:
                    query.setCompleteTime(query.getDateTime());
                    break;
                case QUERY_DATE_TYPE_CHECKOUT_TIME:
                    query.setCheckoutTime(query.getDateTime());
                    break;
                case QUERY_DATE_TYPE_ACCOUNT_TIME:
                    query.setAccountTime(query.getDateTime());
                    break;
                case QUERY_DATE_TYPE_PAY_TIME:
                    query.setPayTime(query.getDateTime());
                    break;
            }
        }

        // 交易类型
        if (Objects.nonNull(query.getTradeType())
                && !Objects.equals(query.getTradeType(), PayConsts.PAY_TRADE_TYPE_REFUND)) {
            query.setNonTradeType(PayConsts.PAY_TRADE_TYPE_REFUND);
            query.setTradeType(null);
        }

        // 不查优惠券支付
        query.setNonPayChannelId(PayConsts.PAY_CHANNEL_PLATFORM_DISCOUNT);

        query.setDeleted(GlobalConsts.NO);
    }

    /**
     * 设置回溯时间
     *
     * @param mv
     */
    protected void setDayLimit(ModelAndView mv) {
        ResponseDTO<List<RoleDRO>> listResponseDTO = roleListRemoteService.listByIds(super.getCurrentStaff().getRoleIds());
        List<RoleDRO> roleDROList = listResponseDTO.getData();
        if (CollectionUtils.isEmpty(roleDROList)) {
            mv.addObject("minTime", "");
            return;
        }
        RoleDRO roleDRO = roleDROList.stream().max((item1, item2) -> {
            if (Objects.isNull(item1.getDayLimit())) {
                return -1;
            }
            if (Objects.isNull(item2.getDayLimit())) {
                return 1;
            }
            if (item1.getDayLimit() > item2.getDayLimit()) {
                return 1;
            } else {
                return -1;
            }
        }).get();

        if (Objects.nonNull(roleDRO.getDayLimit())) {
            Date minDate = DateUtil.addDays(DateUtil.getNow(), -1 * roleDRO.getDayLimit());
            mv.addObject("minTime", DateUtil.toString(minDate, "yyyy-MM-dd 00:00:00"));
        } else {
            mv.addObject("minTime", "");
        }
    }

    /**
     * 获取日期类型
     *
     * @return
     */
    protected List<VtDTO> getDateType() {
        return Lists.newArrayList(
                new VtDTO(QUERY_DATE_TYPE_COMPLETE_TIME, "完成时间"),
                new VtDTO(QUERY_DATE_TYPE_CHECKOUT_TIME, "收单时间"),
                new VtDTO(QUERY_DATE_TYPE_ACCOUNT_TIME, "算账时间"),
                new VtDTO(QUERY_DATE_TYPE_PAY_TIME, "支付时间")
        );
    }

    /**
     * 获取日期类型
     *
     * @return
     */
    protected List<VtDTO> getTradeTypeList() {
        return Lists.newArrayList(
                new VtDTO(PayConsts.PAY_TRADE_TYPE_INTIME, "支付"),
                new VtDTO(PayConsts.PAY_TRADE_TYPE_REFUND, "退款")
        );
    }

    /**
     * 导出权限
     * @param plat
     * @param type
     * @return
     */
    protected Boolean exportPermit(Integer plat, Integer type) {
        String url = "";
        switch (plat) {
            case GlobalConsts.PLAT_MARK_ZMN:
                url = "/zs";
                break;
            case GlobalConsts.PLAT_MARK_YEYX:
                url = "/ys";
                break;
            case GlobalConsts.PLAT_MARK_CNHB:
                url = "/cnhb";
                break;
            default:
                return false;
        }
        url += "/oms/report/orderpay";
        switch (type) {
            // 退款汇总
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST:
                url += "/refund";
                break;
            // 支付明细
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_DETAIL_LIST:
                url += "/detail";
                break;
            default:
                break;
        }
        url += "/list/export.action";
        return StaffAdminService.isPermitted(url);
    }

}
